package com.openbravo.data.gui.selecter;

import java.awt.BorderLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;

import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;

import com.openbravo.data.gui.muleSelecter.MuleComboBoxValEntry;
import com.openbravo.data.gui.muleSelecter.MuleComboxBoxListCellRenderer;

public class JObjectSelecter extends JPanel {

    List<MuleComboBoxValEntry> entrys;
    JList list;
    MuleComboBoxValEntry selected;
    
	/**
	 * Create the panel.
	 */
	public JObjectSelecter(List<MuleComboBoxValEntry> entrys1) {
		setLayout(new BorderLayout(0, 0));
		this.entrys=entrys1;
		JScrollPane scrollPane = new JScrollPane();
		add(scrollPane, BorderLayout.CENTER);
		
		list = new JList();
		scrollPane.setViewportView(list);
		list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		list.setModel(new MyListData(entrys));
		list.addMouseListener(new MouseAdapter(){
			@Override
			public void mouseClicked(MouseEvent e) {
				int selectIndex=list.getSelectedIndex();
				if(selectIndex>=0){
					selected=entrys.get(selectIndex);
				}
			}
		});
		
		list.setCellRenderer(new MuleComboxBoxListCellRenderer());
	}
	
  private static class MyListData extends javax.swing.AbstractListModel {
        
	  List<MuleComboBoxValEntry> m_data;
        
        public MyListData(List data) {
            m_data = data;
        }
        
        public Object getElementAt(int index) {
            return m_data.get(index);
        }
        
        public int getSize() {
            return m_data.size();
        } 
    }
	
	public MuleComboBoxValEntry getSelected(){
		return selected;
	}

}